package com.boot.controller;

import com.boot.entity.Feed;
import com.boot.entity.User;
import com.boot.entity.UserLocation;
import com.boot.service.UserService;
import com.boot.util.AdResult;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Controller
public class UserController {	

	@Resource
	private TokenController tokenController;
	
	@Resource
	private UserService userservice;
	//获取验证码接口
	@RequestMapping("/user/getcode.do")
	@ResponseBody
	public AdResult getCode(String phone){
		AdResult result=userservice.getCode(phone);	
		return result;
	}
	//注册接口
	@RequestMapping("/user/register.do")
	@ResponseBody
	public AdResult register(String username,String phone,String login_password,Integer code) throws Exception{
		AdResult result = userservice.register(username,phone,login_password,code);
		return result;
	}
	//账号密码登陆接口
	@RequestMapping("/user/login.do")
	@ResponseBody
	public AdResult login(String phone,String login_password) throws Exception{
		System.out.println(login_password);
		AdResult result=userservice.login(phone,login_password);
		return result;	
	}
	
	//验证码登陆接口
	@RequestMapping("/user/loginWithoutPwd.do")
	@ResponseBody
	public AdResult loginWithoutPwd(String phone,Integer code,String devicetype){
		AdResult result=userservice.loginWithoutPwd(phone,code,devicetype);
		return result;
	}
	
	//保存用户长连接账号或者绑定长连接账号到用户创建的账号
	@RequestMapping("/user/saveAndBundleUserName.do")
	@ResponseBody
	public AdResult saveAndBundleUserName(String username,String phone,String user_id,String devicetype){
		System.out.println(username);
		AdResult result=userservice.saveAndBundleUserName(username,phone,user_id,devicetype);
		return result;
	}
	//获取用户的详细信息
	@RequestMapping("/user/getUserInfo.do")
	@ResponseBody
	public AdResult getUserInfo(String user_id,String token){
		//在此就执行token的校验
		boolean isOk=tokenController.checkToken(user_id,null,token);
		return isOk?userservice.getUserInfo(user_id,token):AdResult.setResult(null, -1, "验证失败");
	}
	//获取用户的详细信息
	@RequestMapping("/user/getUserInfoByPhone.do")
	@ResponseBody
	public AdResult getUserInfoByPhone(String phone,String token){
		//System.out.println(phone);
		AdResult result;
		boolean isOk=tokenController.checkToken(phone,null,token);
		if(isOk){
			result=userservice.getUserInfoByPhone(phone);
		}else{
			result=AdResult.setResult(null, -1, "验证失败");
		}
		return result;
	}
	//获取用户余额
	@RequestMapping("/user/getBalance.do")
	@ResponseBody
	public AdResult getBalance(String user_id,String token){
		//System.out.println(id);
		boolean isOk=tokenController.checkToken(user_id,null,token);
		return isOk?userservice.getBalance(user_id):AdResult.setResult(null, -1, "验证失败");
	}
	//修改个人信息
	@RequestMapping("/user/update.do")
	@ResponseBody
	public AdResult update(User user,HttpServletRequest request,@RequestParam(value = "file", required = false) CommonsMultipartFile file) throws Exception{
		boolean isOk=tokenController.checkToken(user.getUser_id(),null,user.getToken());
		//AdResult result=userservice.update(user,request,file);
		return isOk?userservice.update(user,request,file):AdResult.setResult(null, -1, "验证失败");
	}
//	//修改个人信息
//	@RequestMapping("/user/updatenoicon.do")
//	@ResponseBody
//	public AdResult update(User user) throws Exception{
//		System.out.println(user);
//		AdResult result=userservice.update(user);
//		return result;
//	}
	//获取用自己发布的广告
	@RequestMapping("/user/getMyAds.do")
	@ResponseBody
	public AdResult getMyAds(String user_id,int pageIndex,int pageSize,String token){
		boolean isOk=tokenController.checkToken(user_id,null,token);
		AdResult result;
		if(isOk){
			result=userservice.getMyAds(user_id, pageIndex, pageSize);
		}else{
			result=AdResult.setResult(null, -1, "验证失败");
		}
		return result;
	}
	//删除广告
	@RequestMapping("/user/deleteAd.do")
	@ResponseBody
	public AdResult deleteAd(String ad_id,String user_id,String token){
		boolean isOk=tokenController.checkToken(user_id,null,token);
		return isOk?userservice.deleteAd(ad_id):AdResult.setResult(null, -1, "验证失败");
//		AdResult result=userservice.deleteAd(ad_id);
//		return result;
	}
	//置顶广告
	@RequestMapping("/user/pushAdTop.do")
	@ResponseBody
	public AdResult pushAdTop(String ad_id,String user_id,String token){
		boolean isOk=tokenController.checkToken(user_id,null,token);
		return isOk?userservice.pushAdTop(ad_id):AdResult.setResult(null, -1, "验证失败");
//		AdResult result=userservice.pushAdTop(ad_id);
//		return result;
	}
	//保存用户位置信息
	@RequestMapping("/user/saveUserLocation.do")
	@ResponseBody
	public AdResult saveUserLocation(UserLocation userLocation,String user_id,String token){
		boolean isOk=tokenController.checkToken(user_id,null,token);
		return isOk?userservice.saveUserLocation(userLocation):AdResult.setResult(null, -1, "验证失败");
//		AdResult result=userservice.saveUserLocation(userLocation);
//		return result;
	}
	//获取用户的职业行业选择
	@RequestMapping("/user/getJobCode.do")
	@ResponseBody
	public AdResult getJobCode(String user_id,String token){
		boolean isOk=tokenController.checkToken(user_id,null,token);
		return isOk?userservice.getJobCode():AdResult.setResult(null, -1, "验证失败");
//		AdResult result=userservice.getJobCode();
//		return result;
	}
	
	
	//修改用户自己定义的广告的顺序
	@RequestMapping("/user/updateMyType.do")
	@ResponseBody
	public AdResult updateMyType(User user){
		boolean isOk=tokenController.checkToken(user.getUser_id(),null,user.getToken());
		return isOk?userservice.updateMyType(user):AdResult.setResult(null, -1, "验证失败");
//		AdResult adResult=userservice.updateMyType(user);
//		return adResult;
	}
	
	@RequestMapping(value="/user/getMyType.do",method=RequestMethod.GET,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getMyType(String user_id,String token){
		//System.out.println(id);
		boolean isOk=tokenController.checkToken(user_id,null,token);
		return isOk?userservice.getMyType(user_id):"获取失败";
//		String myTypeStr=userservice.getMyType(user_id);
//		return myTypeStr;
	}
	//获取根据圈图选中的用户
//	@RequestMapping("/user/getByCircle.do")
//	@ResponseBody
//	public AdResult getByCircle(double lon,double lat,int radius){
//		//System.out.println("123");
//		AdResult adResult = userservice.findAll( lon, lat, radius);
//		return adResult;
//	}
	//根据用户当前位置获取周边用户
	@RequestMapping("/user/getUserLocations.do")
	@ResponseBody
	public AdResult getUserLocations(Double lon,Double lat,Double radius,String user_id,String token){
		System.out.println(lon+":"+lat+":"+radius);
		boolean isOk=tokenController.checkToken(user_id,null,token);
		return isOk?userservice.findAllAround( lon, lat, radius):AdResult.setResult(null, -1, "验证失败");
//		AdResult adResult = userservice.findAllAround( lon, lat, radius);
//		System.out.println(adResult);
//		return adResult;
	}
	
	//根据用户当前位置获取周边用户
	@RequestMapping("/user/lockPay.do")
	@ResponseBody
	public AdResult lockUserPay(String user_id,String token){
		boolean isOk=tokenController.checkToken(user_id,null,token);
		return isOk?userservice.lockUserPay(user_id):AdResult.setResult(null, -1, "验证失败");
//		AdResult adResult = userservice.lockUserPay(user_id);
//		return adResult;
	}
	
	//
	@RequestMapping("/user/isSettingPayPassword.do")
	@ResponseBody
	public AdResult isSettingPayPassword(String user_id,String token){
//		boolean isOk=tokenController.checkToken(user_id,null,token);
//		return isOk?userservice.isSettingPayPassword(user_id,token):AdResult.setResult(null, -1, "验证失败");
		AdResult adResult = userservice.isSettingPayPassword(user_id,token);
		return adResult;
	}
	
	//根据用户当前位置获取周边用户
	@RequestMapping("/user/savePayPassword.do")
	@ResponseBody
	public AdResult savePayPassword(String user_id,String pay_password,String token){
		//System.out.println(pay_password);
		boolean isOk=tokenController.checkToken(user_id,null,token);
		return isOk?userservice.savePayPassword(user_id,pay_password):AdResult.setResult(null, -1, "验证失败");
//		AdResult adResult = userservice.savePayPassword(user_id,pay_password);
//		return adResult;
	}
	
	@RequestMapping("/user/isAssociatedAliPay.do")
	@ResponseBody
	public AdResult isAssociatedAliPay(String user_id,String total_amount,String token){
		AdResult result=userservice.checkIsAssociatedAliPay(user_id, total_amount,token);
		return result;
	}
	//获取奖励广告
	@RequestMapping("/user/getAllMyPushAds.do")
	@ResponseBody
	public AdResult getAllMyPushAds(String user_id,Integer pageIndex,Integer pageSize,String token){
		boolean isOk=tokenController.checkToken(user_id,null,token);
		return isOk?userservice.getAllMyPushAds(user_id,pageIndex,pageSize):AdResult.setResult(null, -1, "验证失败");
//		AdResult adResult=userservice.getAllMyPushAds(user_id,pageIndex,pageSize);
//		return adResult;
	}
	//探雷
	@RequestMapping("/user/getBomb.do")
	@ResponseBody
	public AdResult getBomb(Double lon,Double lat,Double radius,String user_id,String token){
		boolean isOk=tokenController.checkToken(user_id,null,token);
		return isOk?userservice.getBomb(lon,lat,radius):AdResult.setResult(null, -1, "验证失败");
//		System.out.println(lon+":"+lat+":"+radius);
//		AdResult adResult=userservice.getBomb(lon,lat,radius);
//		return adResult;
	}
	
	//获取我的关注
	@RequestMapping("/user/getMyConcern.do")
	@ResponseBody
	public AdResult getMyConcern(String user_id,Integer pageIndex,Integer pageSize,String token){
		boolean isOk=tokenController.checkToken(user_id,null,token);
		return isOk?userservice.getMyConcern(user_id,pageIndex,pageSize):AdResult.setResult(null, -1, "验证失败");
//		AdResult adResult=userservice.getMyConcern(user_id,pageIndex,pageSize);
//		return adResult;
	}
	
	//获取我的关注
	@RequestMapping("/user/getMyCollection.do")
	@ResponseBody
	public AdResult getMyCollection(String user_id,Integer pageIndex,Integer pageSize,String token){
		boolean isOk=tokenController.checkToken(user_id,null,token);
		return isOk?userservice.getMyCollection(user_id,pageIndex,pageSize):AdResult.setResult(null, -1, "验证失败");
//		AdResult adResult=userservice.getMyCollection(user_id,pageIndex,pageSize);
//		return adResult;
	}
	
	
	//反馈不带图
	@RequestMapping("/user/feedBack.do")
	@ResponseBody
	public AdResult feedBack(Feed feed,HttpServletRequest request,@RequestParam(value = "file", required = false) CommonsMultipartFile file,String token){
		boolean isOk=tokenController.checkToken(feed.getUser_id(),null,token);
		return isOk?userservice.feedBack(feed,request,file):AdResult.setResult(null, -1, "验证失败");
//		AdResult adResult=userservice.feedBack(feed,request,file);
//		return adResult;
	}
	
}
